﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Asion.VSIX.Code.Create.Core
{
    /// <summary>
    /// 类 生成模型
    /// </summary>
    public class ClassGenerative
    {



        public ClassGenerative()
        {
            Methods = new List<MethodGenerative>();
        }

        /// <summary>
        /// 命名空间
        /// </summary>
        public string NameSpaceName { get; set; }

        /// <summary>
        /// class
        /// </summary>
        public string ClassName { get; set; }


        /// <summary>
        /// 方法
        /// </summary>
        public List<MethodGenerative> Methods { get; set; }




        /// <summary>
        /// 引用空间
        /// </summary>
        public List<string> UsingPackages { get; set; }


        /// <summary>
        /// 添加引用空间包
        /// </summary>
        /// <param name="package"></param>
        public void AddUsingPackage(string package)
        {
            if (UsingPackages == null)
            {

                UsingPackages = new List<string>();
                UsingPackages.Add(PackageConsts.GRPC_CORE);
                UsingPackages.Add(PackageConsts.SYSTEM);
                UsingPackages.Add(PackageConsts.SYSTEM_COLLECTIONS_GENERIC);

            }
            if (!UsingPackages.Contains(package))
            {
                UsingPackages.Add(package);
            }
        }


        public object ToTemplateData()
        {
            dynamic data = new ExpandoObject();
            data.namespacename = this.NameSpaceName;
            data.classname = this.ClassName;

            dynamic methods = new List<ExpandoObject>();
            foreach (var methodItem in Methods)
            {

                dynamic method = new ExpandoObject();
                method.methodname = methodItem.MethodName;
                method.retruntype = methodItem.RetrunType;

                dynamic parameters = new List<ExpandoObject>();
                foreach (var parameterItem in methodItem.Parameters)
                {
                    dynamic parameter = new ExpandoObject();
                    parameter.parametername = parameterItem.ParameterName;
                    parameter.parametertype = parameterItem.ParameterType;
                    parameters.Add(parameter);
                }
                method.parameters = parameters;
            }
            data.methods = methods;
            return data;


        }

    }

    /// <summary>
    /// 方法 生成模型
    /// </summary>
    public class MethodGenerative
    {
        public MethodGenerative()
        {
            Parameters = new List<ParameterGenerative>();
        }

        /// <summary>
        /// 方法名
        /// </summary>
        public string MethodName { get; set; }

        /// <summary>
        /// 返回类型
        /// <para>string</para>
        /// <para>int</para>
        /// </summary>
        public string RetrunType { get; set; }

        /// <summary>
        /// 设置return type
        /// </summary>
        /// <param name="type"></param>
        public void SetReturnType(string type)
        {
            this.RetrunType = type;
        }


        /// <summary>
        /// 参数集合
        /// </summary>
        public List<ParameterGenerative> Parameters { get; set; }

        public bool IsHasParameters
        {
            get
            {
                return this.Parameters != null && this.Parameters.Count > 0;
            }
        }


    }



    /// <summary>
    /// 参数生成
    /// </summary>
    public class ParameterGenerative
    {
        /// <summary>
        /// 参数名
        /// </summary>
        public string ParameterName { get; set; }

        /// <summary>
        /// 参数类型
        /// </summary>
        public string ParameterType { get; set; }



    }

}
